home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual Basic 5 / Mastering Microsoft Visual Basic 5.ISO / sampapps / globaltimer / ctimer.cls next >
Text File  |  1997-01-14  |  2KB  |  70 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CTimer"
  6. Attribute VB_GlobalNameSpace = True
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = True
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11. Private idTimer As Long
  12. Public Event TimerEvent()
  13.  
  14. ' Start the timer object, if not already running.
  15. Public Function StartTimer(ByVal interval As Long) As Boolean
  16. Attribute StartTimer.VB_Description = "Start a timer associated with the CTimer object. The return value indicated the success or failure of starting the Window's timer."
  17.   If idTimer <> 0 Then
  18.     ' Don't start timer if aleady started.
  19.     StartTimer = False
  20.     Exit Function
  21.   End If
  22.   ' Create new Window's timer and specify the global
  23.   ' TimerProc function as the callback.
  24.   idTimer = SetTimer(0, 0, interval, AddressOf TimerProc)
  25.   If idTimer <> 0 Then
  26.     ' Timer creation succeeded. Add this object to the global collection
  27.     ' of objects with active timers.
  28.     AddTimer Me
  29.     StartTimer = True
  30.   Else
  31.     StartTimer = False
  32.   End If
  33. End Function
  34.  
  35. ' Kill the active timer and clean up. This allows this timer
  36. ' object to be reused simply by allowing the client to re-invoke
  37. ' the StartTimer method.
  38. Public Sub StopTimer()
  39. Attribute StopTimer.VB_Description = "Stop the timer associated with the CTimer object"
  40.   KillTimer 0, idTimer
  41.   
  42.   ' Remove the timer from the global collection. You must reset the
  43.   ' timer ID AFTER removal because the id is used in the RemoveTimer
  44.   ' function.
  45.   RemoveTimer Me
  46.   idTimer = 0
  47. End Sub
  48.  
  49. Private Sub Class_Terminate()
  50.   ' Be sure that if a timer is active that it is killed before this
  51.   ' object is destroyed.
  52.   If idTimer <> 0 Then
  53.     KillTimer 0, idTimer
  54.   End If
  55. End Sub
  56.  
  57. Friend Property Get TimerID() As String
  58.   ' The TimerID property is used by the standard module to retrieve
  59.   ' this object's collection key, which is of type String. Accordingly,
  60.   ' this property is of type String.
  61.   TimerID = Str$(idTimer)
  62. End Property
  63.  
  64. Friend Sub Timer_Event()
  65.   ' This function is called by the TimerProc callback function in the
  66.   ' context of the object that owns the timer that fired. Here, we simply
  67.   ' raise the event to the client(s) of this timer object.
  68.   RaiseEvent TimerEvent
  69. End Sub
  70.